package com.java.jetpackdemo.room

//import androidx.room.Room
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.java.jetpackdemo.R
import kotlinx.android.synthetic.main.activity_room.*

class RoomActivity : AppCompatActivity() {


    private lateinit var viewModel: UserViewModel
    private lateinit var adapter: RoomAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_room)
        initView()
        viewModel = ViewModelProviders.of(this).get(UserViewModel::class.java)
        viewModel.selectAll().observe(this, androidx.lifecycle.Observer<List<UserBean>> {
            adapter.listData.clear()
            adapter.listData.addAll(it)
            adapter.notifyDataSetChanged()
        })

        btn_insert.setOnClickListener {
            viewModel.insert()
        }

        btn_delete.setOnClickListener {
            viewModel.delete()
        }

        btn_update.setOnClickListener {
            viewModel.update()
        }

        btn_deleteAll.setOnClickListener {
            viewModel.deleteAll()
        }
    }

    private fun initView() {
        room_recycler.layoutManager = LinearLayoutManager(this)
        adapter = RoomAdapter()
        room_recycler.adapter = adapter
    }
}
